<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\home\model;
use think\Model;
use think\Db;
class IndexModel extends Model
{
    /*
    * 操作员信息
    * */
    public function session()
    {
        $user=cmf_get_current_user();
        return $user;
    }
    public function __construct($data = [])
    {
        parent::__construct($data);
        $session=$this->session();
        $this->id=$session;
    }
    public function shop()
    {
        $where['status']=1;
        $shop=DB::name('shop')->where($where)->select();
        return $shop;
    }
    public function coupon()
    {
        $user=$this->id;
       if($user)
       {
           $where['voucher_memberid']=$user['m_guid'];
           $where['voucher_use']=0;
           $coupons=DB::name('coupon')->where($where)->select()->toArray();
           $coupon='';
           $date=time();
           foreach ($coupons as $k=>$v)
           {
               $s_time=$v['u_time'];
               $e_time=$v['e_time'];
               if(($date > $s_time||$date==$s_time)&&($date<$e_time||$date==$e_time))
               {
                      $v['past']=1;
               }else{
                        $v['past']=0;
               }
               $coupon[$k]=$v;
           }
        }else{
           $coupon="";
       }

        return $coupon;

    }
    public function  order($array)
    {
        $time=time();
        $user=$this->id;
        if($array['cou_guid'])
        {
            $cou=$this->cou($array,$time);
            $data['actual_money']=$array['money']-$cou['voucher_money'];
            $data['give_money']=$array['money']-$cou['voucher_money'];
        }else{
            $data['actual_money']=$array['money'];
            $data['give_money']=$array['money'];
        }
        $data['or_guid']=guid();
        $data['order_no']=duid();
        $data['member_id']=$user['m_guid'];
        $data['pay_type']=$array['type'];
        $data['shop_id']=$array['re_guid'];
        $data['total_money']=$array['money'];
        $data['c_time']=$time;
        $start=strtotime('today');
        $end=$start+86400;
        $where['c_time']=array('between',array($start,$end));
        $num=DB::name('order')->where($where)->order('num desc')->limit(1)->field('num')->find();
        if($data['c_time']>$start && $data['c_time']<$end)
        {
            $data['num']=$num['num']+1;
        }else{
            $data['num']=0;
        }
        $data['coupon']=$array['cou_guid'];
        $status=DB::name('order')->insert($data);
        if($status)
        {
            $date['od_guid']=guid();
            $date['order_num']=$data['order_no'];
            $date['goods_id']=1;
            $date['goods_name']="石油";
            $date['price']=$array['money'];
            $date['money']=$data['actual_money'];
            if($array['cou_guid'])
            {
                $date['rmb']=$cou['voucher_money'];
            }else{
                $date['rmb']=0;
            }
            $date['shop_id']=$array['re_guid'];
            $date['c_time']=$time;
            $date['num']=1;
            $state=DB::name('order_detail')->insert($date);
            if($state)
            {
                $count['cou']=$state;
                $count['count']=$data;
            }else{
                $count['cou']=0;
            }
        }else{
            $count['cou']=0;
        }
          return $count;
    }
    /*获取优惠金额*/
    public function cou($array,$time)
    {
        $where['cou_guid']=$array['cou_guid'];
        $cou=DB::name('coupon')->where($where)->find();
        $data['voucher_time']=$time;
        $data['voucher_use']=1;
        DB::name('coupon')->where($where)->update($data);
        return $cou;
    }

}